package org.jeecg.modules.iot.model;

import lombok.Data;

@Data
public class KalmanFilter {
    private double q; // 过程噪声
    private double r; // 测量噪声
    private double x; // 值
    private double p; // 估计误差协方差
    private double k; // 卡尔曼增益

    public KalmanFilter(double processNoise, double measurementNoise, double estimate) {
        this.q = processNoise;
        this.r = measurementNoise;
        this.x = estimate;
        this.p = 1;
    }

    public double update(double measurement) {
        // 预测
        p = p + q;

        // 更新
        k = p / (p + r);
        x = x + k * (measurement - x);
        p = (1 - k) * p;

        return x;
    }

    public double getEstimate() {
        return x;
    }
}
